<?php
namespace app\admini\controller;
use app\admini\controller\Base;
use think\Controller;
use think\Db;
use think\request;
use think\Session;

class Student extends Base
{
	
    public function index($keyword='',$tel='',$grade='')
    {
    	$map = array();
		if ($keyword) {
			$map['a.name'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($tel) {
			$map['a.tel'] = $tel;
		}
		$this->assign('tel', $tel);
		if ($grade) {
			$map['a.grade'] = $grade;
		}
		$this->assign('grade', $grade);
		
    	$res = db('oa_student')->alias('a')->field('a.*,w.class_name')->join('oa_classname w','a.grade = w.id')->where($map)->order('a.add_time desc')->paginate(5, false, get_query());
    	$this->assign('data',$res);
    	$type = Db::name('oa_classname')->select();
		$this->assign('type',array2level($type));
		return $this->fetch();
    }
	
	public function add_stu()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$data['add_time']=now_time();//admini
			$res = db('oa_student')->insert($data);
			if($res){
				return $this->toSuccess('/admini/student/index');
			}else{
				return $this->toError();
			}
		}else{
			$res = db('oa_classname')->select();
			$this->assign('type',array2level($res));
			return $this->fetch();	
		}
		
	}
	
	public function updata_stu()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$res = db('oa_student')->updata($data);
			if($res){
				return $this->toSuccess('/admini/student/index');
			}else{
				return $this->toError();
			}
		}else{
			$res = db('oa_student')->find(input('id'));
			$this->assign('data',$res);
			$res = db('oa_classname')->select();
			$this->assign('type',array2level($res));
			return $this->fetch();	
		}
		
	}
	//学分
	public function xuefen($keyword='',$tel='',$grade='')
    {
    	$map = array();
		if ($keyword) {
			$map['a.name'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($tel) {
			$map['a.tel'] = $tel;
		}
		$this->assign('tel', $tel);
		if ($grade) {
			$map['a.grade'] = $grade;
		}
		$this->assign('grade', $grade);
		
    	$res = db('oa_student')->alias('a')->field('a.*,w.class_name')->join('oa_classname w','a.grade = w.id')->where($map)->order('a.add_time desc')->paginate(5, false, get_query());
    	$this->assign('data',$res);
    	$type = Db::name('oa_classname')->select();
		$this->assign('type',array2level($type));
		return $this->fetch();
    }
    //奖惩
	public function jiangcheng($keyword='',$tel='',$grade='')
    {
    	$map = array();
		if ($keyword) {
			$map['a.name'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($tel) {
			$map['a.tel'] = $tel;
		}
		$this->assign('tel', $tel);
		if ($grade) {
			$map['a.grade'] = $grade;
		}
		$this->assign('grade', $grade);
		
    	$res = db('oa_student')->alias('a')->field('a.*,w.class_name')->join('oa_classname w','a.grade = w.id')->where($map)->order('a.add_time desc')->paginate(5, false, get_query());
    	$this->assign('data',$res);
    	$type = Db::name('oa_classname')->select();
		$this->assign('type',array2level($type));
		return $this->fetch();
    }
    
    //回家登记
	public function dengji($keyword='',$tel='',$grade='')
    {
    	$map = array();
		if ($keyword) {
			$map['a.name'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($tel) {
			$map['a.tel'] = $tel;
		}
		$this->assign('tel', $tel);
		if ($grade) {
			$map['a.grade'] = $grade;
		}
		$this->assign('grade', $grade);
		
    	$res = db('oa_student')->alias('a')->field('a.*,w.class_name')->join('oa_classname w','a.grade = w.id')->where($map)->order('a.add_time desc')->paginate(5, false, get_query());
    	$this->assign('data',$res);
    	$type = Db::name('oa_classname')->select();
		$this->assign('type',array2level($type));
		return $this->fetch();
    }
    
	//导出
	public function excel_dc(){
 		//导入相关文件
//		require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/PHPExcel/PHPExcel.php';
//		require './vendor/PHPExcel/PHPExcel.php';
		//方法一
		vendor("PHPExcel.PHPExcel");   
		//实例化
		$phpexcel = new \PHPExcel();
		//设置比标题
		$phpexcel->getActiveSheet()->setTitle('学生信息');
		//设置表头
		$phpexcel->getActiveSheet() ->setCellValue('A1','姓名')
		                            ->setCellValue('B1','性别')
		                            ->setCellValue('C1','电话')
		                            ->setCellValue('D1','家长电话')
		                            ->setCellValue('E1','地址')
		                            ->setCellValue('F1','出生年月')
		                            ->setCellValue('G1','班级')
		                            ->setCellValue('H1','入校时间');
		//从数据库取得需要导出的数据
		$list = db('oa_student')->alias('a')
		->field('a.*,w.class_name')
		->join('oa_classname w','a.grade = w.id')
		->order('a.add_time desc')->select();
		//用foreach从第二行开始写数据，因为第一行是表头
		$i=2;
		foreach($list as $val){
			
   	 		$phpexcel->getActiveSheet() ->setCellValue('A'.$i,' '.$val['name'])
                            ->setCellValue('B'.$i, $val['sex'])
                            ->setCellValue('C'.$i, $val['username'])
                            ->setCellValue('D'.$i, $val['tel'])
                            ->setCellValue('E'.$i, $val['address'])
                            ->setCellValue('F'.$i, $val['age'])
                            ->setCellValue('G'.$i, $val['class_name'])
                            ->setCellValue('H'.$i, $val['in_time']);
		    $i++;
		}
		
		$obj_Writer = \PHPExcel_IOFactory::createWriter($phpexcel,'Excel5');
		$filename ='学生信息'. date('Y-m-d').".xls";//文件名
		
//		$obj_Writer->save($filename);
		
		//设置header
		header("Content-Type: application/force-download"); 
		header("Content-Type: application/octet-stream"); 
		header("Content-Type: application/download"); 
		header('Content-Disposition:inline;filename="'.$filename.'"'); 
		header("Content-Transfer-Encoding: binary"); 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
		header("Pragma: no-cache"); 
		$obj_Writer->save('php://output');//输出		
		die();//种植执行
 	}
	
	/**
	 * 导入信息
	 * @param $id
	 */
	public function export_xl() {
		
		return $this->fetch();
	}
	
	public function saveImport(){  
        //import('phpexcel.PHPExcel', EXTEND_PATH);//方法二  
        vendor("PHPExcel.PHPExcel"); //方法一  
        $objPHPExcel = new \PHPExcel();  
  
        //获取表单上传文件  
        $file = request()->file('excel');  
        $info = $file->validate(['ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');  
        if($info){  
            $exclePath = $info->getSaveName();  //获取文件名  
            $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath;   //上传文件的地址  
            $objReader =\PHPExcel_IOFactory::createReader('Excel5');  
            $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8  
           // echo "<pre>";  
            $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式  
            array_shift($excel_array);  //删除第一个数组(标题);  
            $data = [];  
            $i=0;  
           
            foreach($excel_array as $k=>$v) {  
            	if($v[0]){
            	$data[$k]['cate_id1'] = $v[0]; 
                $data[$k]['cate_id2'] = $v[1];
                $data[$k]['title'] = $v[2];
                $data[$k]['market'] = $v[3];
                $data[$k]['maxprice'] = $v[4]; 
                $data[$k]['minprice'] = $v[5]; 
                $data[$k]['pprice'] = $v[6]; 
                $data[$k]['rose'] = $v[7]; 
                $data[$k]['add_time'] = $v[8];	
                $data[$k]['update_time'] = now_time();
            	}
                $i++;  
            }  
            //dump($data);
           $success=db('newsjg')->insertAll($data); //批量插入数据  
           //$i=  
           $error=$i-$success;  
            echo "总{$i}条，成功{$success}条，失败{$error}条。";  
           // Db::name('t_station')->insertAll($city); //批量插入数据  
        }else{  
            // 上传失败获取错误信息  
            echo $file->getError();  
        }  
  
    } 

}
